var currentTab:String;
var testFlag:Boolean;

function init(param, data, slot)
{
	Packages.model.Context.activeContent=this;

	// Condition.check
	if (Packages.model.UserProfile.RecordingCollabId==null) 
	{
		Packages.components.SceneAgent.loadScene("content/HomePage", true);
		return;
	}
	
	// UI.init
	var basic=Packages.system.mlf.GateKeeper.scanElement(this._name, "basic");
	Packages.system.display.JAccordion.initialize(basic);
	
	// Render.init
	Packages.system.render.Filter.titleFilter(Packages.system.mlf.GateKeeper.scanElement(this._name, "prepairationTitle"));
	Packages.system.render.Filter.titleFilter(Packages.system.mlf.GateKeeper.scanElement(this._name, "myReadjoyTitle"));
	Packages.system.display.SceneTransition.rollOut();

	// Event.init
	var evtTargetArr:Array=new Array();
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "Prepairation_print"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "OnAir_print"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "PostProduction_print"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "Publish_print"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "back_print"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "startSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "confirmSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "applyFormatSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "setBlockSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "nextBlockSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "prevBlockSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "sendMsgSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "switchInfoSpot"));
	Packages.system.events.StateFulButtonEvent.initialize(evtTargetArr);

	invokeInitComplete();
}

function dispose()
{
	Packages.system.mlf.GateKeeper.removeListener("onReleaseButtonEvent", this);
	Packages.system.mlf.GateKeeper.removeListener("onRTMPConnected", this);
	Packages.system.mlf.GateKeeper.removeListener("onLadderChange", this);
	Packages.system.mlf.GateKeeper.removeListener("sliderChange", this);
	Packages.system.mlf.GateKeeper.removeListener("onColorPickerChanged", this);

	delete currentTab;
	delete testFlag;

	Packages.modula.RTxtReader.removeInstance();
	// remove mic status listener
	var active_mic:Microphone=Microphone.get();
	active_mic.onActivity=null;

	Packages.modula.MicrophoneDevice.close();

	// Packages.modula.task.Common.stopCollabEnvironment();
}

function invokeInitComplete()
{
	Packages.system.mlf.GateKeeper.addListener("onReleaseButtonEvent", this);
	Packages.system.mlf.GateKeeper.addListener("sliderChange", this);
	Packages.system.mlf.GateKeeper.addListener("onLadderChange", this);
	Packages.system.mlf.GateKeeper.addListener("onColorPickerChanged", this);
	
	// clear or not
	Packages.modula.task.Composition.clear();
	
	// select the first tab as default
	var micTabSpot=Packages.system.mlf.GateKeeper.scanElement(this._name, "mictab");
	var param=mlf.mcmanager.PathResolver.drawPath(micTabSpot);
	Packages.system.events.StateFulButtonEvent.invokeReleaseEvent(param, data, this._name);

	//
	initTxtReader();
	// check the collab env and initialize the env
	if (Packages.model.UserProfile.RecordingCollabId==null)
	{
		// Packages.modula.task.Common.checkCollabEnvironment(this._name);
		// append basic info with no collab env
	}
	else Packages.modula.task.Common.startCollabEnvironment(this._name);
	
	Packages.modula.task.Common.establishAudioChannel();
	
	// mic.init

	Packages.modula.task.Common.prefetchCurrentTaskContext();
}

/*function prefetchCurrentTaskContext()
{
	var task=Packages.modula.task.Composition.currentTask;
	/*for (var a in task)
	{
		Packages.log(a+": "+task[a], "debug");
	}
		[DEBUG]: publishedAudioId: 2
		[DEBUG]: chapter.chapterId: 2
		[DEBUG]: recordTaskId: 2
		[DEBUG]: recordAlbum.recordAlbumId: 1
		[DEBUG]: status: 1
		[DEBUG]: updateTime: 2008-09-30 21:43:17.0
		[DEBUG]: latestAudioId: 2
		[DEBUG]: createTime: 2008-09-30 21:43:29.0
		*?
	var hintArea_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "hintArea");
	if (task==null)
	{
		hintArea_tf.text="To create the new task.";
	}
	else 
	{
		for (var a in task)
		{
			hintArea_tf.text+=a+": "+task[a]+"\n";
			hintArea_tf.scroll=hintArea_tf.maxscroll;
		}
	}
}*/

function onLadderChange(evt:Object)
{
	var ladder=mlf.gatekeeper.native.Caller.scanElement(this._name, "aladder");
	ladder.jnladder.pos=evt.param;
	// Packages.log("onActivity: "+evt.param+" "+evt.active, "debug");
	ladder.jnladder.draw();
}

function onColorPickerChanged(obj)
{
	var colorTf:TextField;
	var path=mlf.mcmanager.PathResolver.drawPath(obj.ins);
	if (path.indexOf("currentCp")>-1)
	{
		colorTf=mlf.gatekeeper.native.Caller.scanElement(this._name, "colorInput");
	}
	else if (path.indexOf("appendCp")>-1)
	{
		colorTf=mlf.gatekeeper.native.Caller.scanElement(this._name, "acolorInput");
	}
	colorTf.text="0x"+obj.param;
}

function sliderChange(evtObj:Object)
{
	Packages.log("sliderChange: ~~", "debug");
	
	var target=mlf.mcmanager.PathResolver.drawPath(evtObj.target);
	if (target.toString().indexOf("gslider")>-1)
	{
		Packages.modula.MicrophoneDevice.setGain(evtObj.param);
	}
	else if (target.toString().indexOf("rslider")>-1)
	{
		var rate:Number;
		switch (Math.floor(evtObj.param/20)){
			case 0:
				rate=5;
				break;
			case 1:
				rate=8;
				break;
			case 2:
				rate=11;
				break;
			case 3:
				rate=22;
				break;
			case 4:
			case 5:
				rate=44;
				break;
		}
		Packages.modula.MicrophoneDevice.setRate(rate);
	}

	if (testFlag) Packages.modula.MicrophoneDevice.publish(Packages.modula.task.Composition.audioId, "live");
}

function showMessage(mess:String)
{
	var hintArea_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "hintArea");
	hintArea_tf.htmlText+=mess;
}

function initTxtReader()
{
	var portlet=mlf.gatekeeper.native.Caller.scanElement(this._name, "txtReader");
	Packages.modula.RTxtReader.getInstance(portlet);

	var docXML=new XML();
	docXML.ignoreWhite=true;
	docXML.onLoad=function()
	{
		Packages.modula.task.Composition.content=this.toString();
		Packages.modula.RTxtReader.display(Packages.modula.task.Composition.content);
	}

	// var loadPath:String="http://www.readjoy.cn:1080/repo/txt/"+Packages.modula.task.Composition.contentId+".txt";
	// http://www.readjoy.cn:1080/jumpingNotes/download?uuid=01&type=0&subtype=0 
	// TODO: Add to config file
	var loadPath:String=Packages.system.mlf.GateKeeper.accessSystemConfig("httpRequest")+"/download?uuid="+Packages.modula.task.Composition.contentId+"&type=0&subtype=0";
	Packages.log("loadPath: "+loadPath, "debug");
	docXML.load(loadPath);
}

function switchTab(newTabName:String)
{
	var currentTabPt=Packages.system.mlf.GateKeeper.scanElement(this._name, currentTab);
	currentTabPt._visible=false;
	var newTabPt=Packages.system.mlf.GateKeeper.scanElement(this._name, newTabName);
	newTabPt._visible=true;

	currentTab=newTabName;
}

function switchInfo()
{
	var hintDisplayContainer=Packages.system.mlf.GateKeeper.scanElement(this._name, "hintDisplayContainer");
	if (hintDisplayContainer._visible)
	{
		Packages.system.mlf.GateKeeper.updatePortletView(this._name, "hintDisplayContainer", "infoDisplayContainer");
	}
	else Packages.system.mlf.GateKeeper.updatePortletView(this._name, "infoDisplayContainer", "hintDisplayContainer");
}

function startTest()
{
	testFlag=true;
	
	if (Packages.modula.task.Composition.audioId==null)
	{
		return;
	}
	
	Packages.modula.MicrophoneDevice.publish(Packages.modula.task.Composition.audioId, "live");
	Packages.modula.MicrophoneDevice.subscribe(Packages.modula.task.Composition.audioId);
}

function confirmTest()
{
	testFlag=false;

	Packages.modula.MicrophoneDevice.saveMicSettings();
	Packages.modula.MicrophoneDevice.close();
}

function applyFormat()
{
	var fmtObj=new Object();
	var sizeInput_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "sizeInput");
	if (sizeInput_tf.text!="") fmtObj.size=Number(sizeInput_tf.text);
	var colorInput_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "colorInput");
	if (colorInput_tf.text!="") fmtObj.color=Number(colorInput_tf.text);
	var leadInput_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "leadInput");
	if (leadInput_tf.text!="") fmtObj.leading=Number(leadInput_tf.text);
	var afmtObj=new Object();
	var asizeInput_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "asizeInput");
	if (asizeInput_tf.text!="") afmtObj.size=Number(asizeInput_tf.text);
	var acolorInput_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "acolorInput");
	if (acolorInput_tf.text!="") afmtObj.color=Number(acolorInput_tf.text);
	var aleadInput_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "aleadInput");
	if (aleadInput_tf.text!="") afmtObj.leading=Number(aleadInput_tf.text);
	var fmtParam=new Object();
	fmtParam.currentTextFormat=fmtObj;
	fmtParam.appendTextFormat=afmtObj;
	Packages.modula.task.Composition.readerSettings.fmt=fmtParam;
	Packages.modula.RTxtReader.applyFormat(fmtParam);
}

function setBlock()
{
	var blocksizeInput_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "blocksizeInput");
	var blockspeedInput_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "blockspeedInput");
	var blockParam=new Object();
	blockParam.size=blocksizeInput_tf.text;
	blockParam.speed=blockspeedInput_tf.text;
	Packages.modula.task.Composition.readerSettings.block=blockParam;
	Packages.modula.RTxtReader.setBlock(blockParam, true);
}

function nextBlock()
{
	Packages.modula.RTxtReader.switchBlock(1);
}

function prevBlock()
{
	Packages.modula.RTxtReader.switchBlock(-1);
}

function onReleaseButtonEvent(evt:Object)
{
	// Three types operation for collab env
	// private operation: tab, sendMsgSpot(public), startSpot, confirmSpot, applyFormatSpot, setBlockSpot, prevBlockSpot, nextBlockSpot, switchInfoSpot, print(host)
	// public operation: none
	// host operation: none
	
	if (evt.param.indexOf("tab")>-1)
	{
		switchTab(evt.param+"pt");
	}
	else if (evt.param=="sendMsgSpot")
	{
		Packages.modula.task.Common.sendMsg();
	}
	else if (evt.param=="startSpot")
	{
		startTest();
	}
	else if (evt.param=="confirmSpot")
	{
		confirmTest();
	}
	else if (evt.param=="applyFormatSpot")
	{
		applyFormat();
	}
	else if (evt.param=="setBlockSpot")
	{
		setBlock();
	}
	else if (evt.param=="prevBlockSpot")
	{
		prevBlock();
	}
	else if (evt.param=="nextBlockSpot")
	{
		nextBlock();
	}
	else if (evt.param=="switchInfoSpot")
	{
		switchInfo();
	}
	else if (evt.param.indexOf("print")>-1)
	{
		if (Packages.model.UserProfile.recordingCollabFlag && !Packages.modula.task.Composition.host)
		{
			Packages.modula.MessageCenter.send("YOU_HAVE_NO_RIGHTS_TO_DO_HOST_OPERATION", 1, 0);
			return;
		}
		
		var printName=evt.param.substr(0, evt.param.length-6);
		Packages.modula.task.Common.switchRoom(printName, this._name);
	}
}